package javaMSCRM;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.microsoft.schemas.crm._2006.query.AllColumns;
import com.microsoft.schemas.crm._2006.webservices.Key;
import com.microsoft.schemas.crm._2007.webservices.Activitypointer;
import com.microsoft.schemas.crm._2007.webservices.CreateDocument;
import com.microsoft.schemas.crm._2007.webservices.CreateResponseDocument;
import com.microsoft.schemas.crm._2007.webservices.DeleteDocument;
import com.microsoft.schemas.crm._2007.webservices.EntityName;
import com.microsoft.schemas.crm._2007.webservices.RetrieveDocument;
import com.microsoft.schemas.crm._2007.webservices.TargetUpdateEmail;
import com.microsoft.schemas.crm._2007.webservices.UpdateDocument;
import com.microsoft.schemas.crm._2007.webservices.UpdateResponseDocument;
import com.microsoft.schemas.crm._2007.webservices.CreateDocument.Create;
import com.microsoft.schemas.crm._2007.webservices.CreateResponseDocument.CreateResponse;
import com.microsoft.schemas.crm._2007.webservices.DeleteDocument.Delete;
import com.microsoft.schemas.crm._2007.webservices.RetrieveDocument.Retrieve;
import com.microsoft.schemas.crm._2007.webservices.UpdateDocument.Update;

import net.miginfocom.layout.AC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;

/**
 * 
 */

/**
 * @author Zoran IVANOV
 *
 */
public class JavaMSCRMActivity extends Form {
	JavaMSCRM JavaMSCRM;
	Activitypointer record = null;
	String EntityId = "";
	JTextField 	JTFSubject=new JTextField(""),
				JTFActivityType=new JTextField("");
	JButton SaveButton,DeleteButton;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public JavaMSCRMActivity(JavaMSCRM JavaMSCRM) {
		super(JavaMSCRM);
		setTitle("New Activity");
		JScrollPane FormScrollPane = new JScrollPane(setupFields());
		FormScrollPane.setBorder(BorderFactory. createEmptyBorder(0,0,0,0));
		add(FormScrollPane, BorderLayout.CENTER);
		pack();
		setVisible(true);
	}
	
	public JavaMSCRMActivity(JavaMSCRM JavaMSCRM, String Id) {
		super(JavaMSCRM, Id);
		try {
	        RetrieveDocument rd = RetrieveDocument.Factory.newInstance();  
	        Retrieve r = Retrieve.Factory.newInstance();	        
	        r.setEntityName(EntityName.ACTIVITYPOINTER.toString());
	        r.setId(Id);
	        r.setColumnSet(AllColumns.Factory.newInstance());	        
	        rd.setRetrieve(r);
	        record = (Activitypointer) JavaMSCRM.stub.Retrieve(rd,  JavaMSCRM.catd, null, null).getRetrieveResponse().getRetrieveResult();
		}  
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.toString());
		}		
		setTitle("Activity: " + record.getSubject());
		JScrollPane FormScrollPane = new JScrollPane(setupFields());
		FormScrollPane.setBorder(BorderFactory. createEmptyBorder(0,0,0,0));
		add(FormScrollPane, BorderLayout.CENTER);
		buttonsBar.setVisible(false);
		pack();
		setVisible(true);
	}
	
	private JPanel setupFields(){
		LC layC = new LC().wrap();
		AC colC = new AC().align("right", 0).fill(1, 3).grow(100, 1, 3).align("right", 2).gap("15", 1);
		AC rowC = new AC().index(6).gap("15!").align("top").grow(100, 0);	
		JPanel form = new JPanel(new MigLayout(layC, colC, rowC));		
		JPanel formContainer = new JPanel(new MigLayout(layC, colC, rowC));		
		formContainer.add(form, BorderLayout.NORTH);			
		form.setBorder(BorderFactory. createEmptyBorder(0,0,0,0));
		//Create TextFields
		
		try{JTFSubject = new JTextField(record.getSubject());}catch (Exception e) {}
		try{JTFActivityType = new JTextField(record.getActivitytypecode().getStringValue());}catch (Exception e) {}
		
		JTFSubject.setEnabled(false);
		JTFActivityType.setEnabled(false);
		
		addSeparator(form, "General");		
		
		form.add(new JavaMSCRMLabel("Topic", true));		
		form.add(JTFSubject, "w 200:100:");
		form.add(new JavaMSCRMLabel("Activity Type", true));
		form.add(JTFActivityType, "w 200:100:");
				
		return formContainer;
	}
	
	public void createRecord(){
		String EntityId = "";
		try {
			TargetUpdateEmail targetEntity = TargetUpdateEmail.Factory.newInstance();
			Activitypointer Entity = (Activitypointer)targetEntity.addNewEmail();
			
			Entity.setSubject(JTFSubject.getText());
			//Entity.setActivitytypecode(JTFActivityType.getText());
						
			CreateDocument cd = CreateDocument.Factory.newInstance();
			Create create = Create.Factory.newInstance();
			create.setEntity(Entity);
			cd.setCreate(create);
		
			CreateResponseDocument crd = JavaMSCRM.stub.Create(cd, JavaMSCRM.catd, null, null);
			CreateResponse cr = crd.getCreateResponse();
			EntityId = cr.getCreateResult();
		}  
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.toString());
		}
		
		if(EntityId != ""){
			new Activities(JavaMSCRM);
			new JavaMSCRMActivity(JavaMSCRM,EntityId);
			dispose();
		}else{
			JOptionPane.showMessageDialog(null, "There was an error while creating the record!");
		}		
	}
	
	public void updateRecord(){
		try {
			TargetUpdateEmail targetEntity = TargetUpdateEmail.Factory.newInstance();
			Activitypointer Entity = (Activitypointer)targetEntity.addNewEmail();

			Entity.setSubject(JTFSubject.getText());
			//Entity.setActivitytypecode(JTFActivityType.getText());
			
			Key id = Key.Factory.newInstance();
			id.setStringValue(this.EntityId);
			Entity.setActivityid(id);
			
			UpdateDocument ud = UpdateDocument.Factory.newInstance();
			Update update = Update.Factory.newInstance();
			update.setEntity(Entity);
			ud.setUpdate(update);
		
			UpdateResponseDocument urd = this.JavaMSCRM.stub.Update(ud, JavaMSCRM.catd, null, null);
			urd.getUpdateResponse();
		}  
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.toString());
		}
		
		new Activities(JavaMSCRM);
		new JavaMSCRMActivity(this.JavaMSCRM,this.EntityId);
		dispose();
	}
	
	public void deleteRecord(){
		String msg ="Record succesfuly deleted";
		int deleteConfirmation = JOptionPane.showConfirmDialog(null,
				"Are you sure you want to delete this record?",
	       		"Record Deletion Confirmation", 
		        JOptionPane.YES_NO_OPTION);
		if (deleteConfirmation == JOptionPane.YES_OPTION){
			try {
				DeleteDocument dd = DeleteDocument.Factory.newInstance();
				Delete d = Delete.Factory.newInstance();
				d.setId(this.EntityId);
				d.setEntityName(EntityName.ACTIVITYPOINTER.toString());
				
				dd.setDelete(d);
				
				JavaMSCRM.stub.Delete(dd, JavaMSCRM.catd, null, null);
			}  
			catch (Exception e) {
				msg ="There was a problem while deleting some of the record!";
			}
			
			new Activities(JavaMSCRM);
			dispose();
			JOptionPane.showMessageDialog(null, msg);
		}
	}
}
